/**
 * Created by IntelliJ IDEA.
 * @Author: xuybang
 * @version: 2017/5/25.
 * To change this template use File | Settings | File Templates.
 */
(function () {

    var $view;
    var $template;

    function onInit() {

        onInitData();

        onVeriUserInfo();

    }


    function onInitData() {
        $template = $view.find('.user_chat_list').children('.template');
        $template.remove().removeClass("template");
    }

    function onVeriUserInfo() {
        var user = backService.localLoginInfo;
        // console.log(user);
        if (user != null && user.id != null) {
            onShowUserList();
        }else{

        }
    }

    // 获取您的好友列表
    function onShowUserList() {
        backService.message.getUserFriends(backService.localLoginInfo.id, function (json) {
			// console.log(json.data);
            var dataObject = json.data;
            if (dataObject.length > 0) {
                var $ul = $view.find('.user_chat_list');
                for (var i in dataObject) {
                    var data = dataObject[i];
                    var $item = $template.clone();
                    var src = backService.getFileUrl(data.avatar, "img/message/show/head.jpg");
                    $item.find('img.chat_avatar_img').attr("src", src);
                    $item.find('img.chat_avatar_img').data("id", data.user.id)
                    $item.find('a.chat_link').attr("href", "/message/show.html?id=" + data.user.id + "&name=" + data.user.nickName);
                    $item.find('a.chat_link .username').text(data.user.nickName);
                    $item.find('a.chat_link .msg_con').text(data.content);
                    $item.appendTo($ul);
                }
            }
        }, function (error) {
            app.showToast(error.msg);
        });
    }

    /*初始化*/
    window.onViewInit_message_index = function (view) {
        $view = view.element;

        onInit();
    };

    window.onViewShow_message_index = function (view) {
        $view = view.element;

        app.onBackTabItemActive($view, 3);
    };
})();